package com.wulis.common.handler;

import com.wulis.common.exception.BusinessException;
import com.wulis.common.wrapper.ApiWrapMapper;
import com.wulis.common.wrapper.ApiWrapper;
import io.swagger.v3.oas.annotations.Hidden;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author wuligao
 */
@Slf4j
@Hidden
@RestControllerAdvice
public class ApiExceptionHandler {

    /**
     * 业务异常
     *
     * @param e BusinessException
     * @return ApiWrapper<Object>
     */
    @ExceptionHandler(BusinessException.class)
    public ApiWrapper<?> exceptionHandler(BusinessException e) {
        log.error(e.getMessage());
        return ApiWrapMapper.wrap(e.getCode(), e.getMessage());
    }

    /**
     * 最大的exception异常拦截要放到最后
     *
     * @param e Exception
     * @return ApiWrapper<Object>
     */
    @ExceptionHandler(Exception.class)
    public ApiWrapper<?> exceptionHandler(Exception e) {
        log.error(e.getMessage());
        return ApiWrapMapper.error("内部异常:" + e.getMessage());
    }

}
